package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.Method;
import java.util.*;

@Controller
@RequestMapping("/permission")
public class PermissionController {


    @Autowired
    private ApplicationContext ctx;

    @Autowired
    private IPermissionService permissionService;

    // 处理权限查询分页请求 /permission/list
    @RequestMapping("/list")
    public String list(Model model, QueryObject qo) {
        PageInfo<Permission> pageInfo = permissionService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "permission/list"; // /WEB-INF/views/permission/list.ftl

        // 获取容器所有控制器
        // ctx.getBeansWithAnnotation(Controller.class);
    }

    // 处理删除请求 /permission/delete?id=1
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
            permissionService.delete(id);
        }

        return "redirect:/permission/list"; // localhost/permission/list
    }

    @RequestMapping("/load")
    @ResponseBody
    public JsonResult load() {

        try {
            // 如何知道所有控制器的处理方法, 转成成数据库权限表中的一条条数据
            Map<String, Object> map = ctx.getBeansWithAnnotation(Controller.class);

            // 定义一个存权限 List 集合
            Set<Permission> permissions = new LinkedHashSet<>();

            // 查询数据库所有全新啊
            List<Permission> allPermissions = permissionService.listAll();

            // 获取所有控制器对象
            Collection<Object> controllers = map.values();
            for (Object controller : controllers) {
                // 获取控制器对象本类中的方法
                Method[] methods = controller.getClass().getDeclaredMethods();
                for (Method method : methods) {
                    // 获取方法的注解
                    RequiredPermission annotation = method.getAnnotation(RequiredPermission.class);
                    if (annotation != null) {
                        Permission permission = new Permission();
                        permission.setName(annotation.name());
                        permission.setExpression(annotation.expression());

                        // 存到 set 之前判断一下, 看这个权限是否在数据库已存在, 若不存在才存到 set, 之后插入
                        if (!allPermissions.contains(permission)) {
                            permissions.add(permission);
                        }
                    }
                }
            }
            if (permissions.size() > 0) { // 没有数据导致 sql 语法错误
                permissionService.batchSave(permissions);
            }
            return new JsonResult(true, "加载成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "加载失败");
        }
    }

    // 处理保存或者修改请求 /permission/saveOrUpdate 参数在请求体
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Permission permission) {
        if (permission.getId() == null) {
            permissionService.save(permission);
        } else {
            permissionService.update(permission);
        }
        return "redirect:/permission/list"; // localhost/permission/list
    }

}
